<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/12/24
+--------------------------------------------------------------
 * Time: 10:38
+--------------------------------------------------------------
 * 短消息
+--------------------------------------------------------------
 */
class MailModel extends BaseModel{

    protected $tableName = "mail";//数据库表名

    protected $_validate = array(
        array('to_user','require','不能为空'),
        array('from_user','require','请登录')
    );

    /**
     * @param $post
     * @return array
     * 新建短信息
     */
    public function sendMail($post){
        $return = array('status'=>'','info'=>'');
        $data = array(
            'mail_id'=>null,
            'to_user'       =>$post['to_user'],
            'from_user'     =>$_SESSION['user_id'],
            'title'         =>$post['title'],
            'content'       =>$post['content'],
            'new_mail'      =>1,
            'reply'         =>0,
            'in_date'       =>date("Y-m-d H:i:s"),
            'defunct'       =>"N"
        );
        if($this->create($data)){
            $res = $this->add($data);
            if($res){
                $return['status'] = true;
                $return['info']   = "发送成功";
            }else{
                $return['status'] = false;
                $return['info']   = $this->getDbError();
            }
        }else{
            $return['status'] = false;
            $return['info']   = $this->getError();
        }
        return $return;
    }
    public function getMailCount(){
        $to_user = $_SESSION['user_id'];
        if(empty($to_user)){
            return 0;
        }
        $where = " to_user='".$to_user."' and new_mail=1 and defunct='N'";
        $total = $this->where($where)->count();
        return intval($total);
    }

}